﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Nefela.BaseClasses
{
    public class ActionListItem
    {
        private Image image = null;
        private Boolean enabled = true;
        private String text = String.Empty;


        private List<Component> lcntrl = new List<Component>();
        private void ModImage()
        {
            foreach (var item in lcntrl)
            {
                if (item == null)
                {
                    lcntrl.Remove(item);
                    continue;
                }

                if ((item as ButtonBase) != null)
                    (item as ButtonBase).Image = image;

                if ((item as ToolStripItem) != null)
                    (item as ToolStripItem).Image = image;
            }
        }

        private void ModEnabled()
        {
            foreach (var item in lcntrl)
            {
                if (item == null)
                {
                    lcntrl.Remove(item);
                    continue;
                }

                if ((item as ButtonBase) != null)
                    (item as ButtonBase).Enabled = enabled;

                if ((item as ToolStripItem) != null)
                    (item as ToolStripItem).Enabled = enabled;
            }
        }

        public Boolean Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                ModEnabled();
            }
        }

        public Action ExecutedAction { get; set; }

        public Image ImageControl
        {
            get { return image; }
            set
            {
                image = value;
                ModImage();
            }
        }

        public String Text
        {
            get { return text; }
            set
            {
                text = value;

                foreach (var item in lcntrl)
                {
                    if (item == null)
                    {
                        lcntrl.Remove(item);
                        continue;
                    }

                    if ((item as ButtonBase) != null)
                        (item as ButtonBase).Text = text;

                    if ((item as ToolStripItem) != null)
                        (item as ToolStripItem).Text = text;
                }
            }
        }

        public void AddControl(Component AddedControl)
        {
            if (lcntrl.Contains(AddedControl))
                return;

            var tsi = AddedControl as ToolStripItem;
            if (tsi != null)
            {
                tsi.Click += AddedControl_Click;
                tsi.Image = ImageControl;
                tsi.Enabled = enabled;
                tsi.Text = text;
                lcntrl.Add(tsi);
            }

            var cntr = AddedControl as ButtonBase;
            if (cntr != null)
            {
                cntr.Click += AddedControl_Click;
                cntr.Image = ImageControl;
                cntr.Enabled = enabled;
                cntr.Text = text;
                lcntrl.Add(cntr);
            }


        }

        void AddedControl_Click(object sender, EventArgs e)
        {
            if (ExecutedAction != null)
            ExecutedAction();
        }




    }
}
